/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apirose;

import apirose.APICours;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v-tech_master
 */
public class Cours_MySQL implements APICours {
    
    private Request_MySQL bdd;

    Cours_MySQL(Request_MySQL request) {
        this.bdd = request;
    }

    public ArrayList<Integer> getidSeanceCoursBDD(int idUE) {
        ArrayList<Integer> listeId = new ArrayList<Integer>();
        int idType;
        
        String req = "SELECT id FROM Type_Cours WHERE nom='Cours';";
        ResultSet rs = bdd.executer_requete(req);
        try {
            rs.next();
            idType = rs.getInt("id");
        
            req = "SELECT id FROM Seance WHERE idUE = "+idUE+" AND idTypeCours="+idType+";";

            rs = bdd.executer_requete(req);
            while(rs.next()){
                listeId.add(rs.getInt("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listeId;
    }

    public ArrayList<Integer> getidSeanceTdBDD(int idUE) {
        ArrayList<Integer> listeId = new ArrayList<Integer>();
        int idType;
        
        String req = "SELECT id FROM Type_Cours WHERE nom='TD';";
        ResultSet rs = bdd.executer_requete(req);
        try {
            rs.next();
            idType = rs.getInt("id");
        
            req = "SELECT id FROM Seance WHERE idUE = "+idUE+" AND idTypeCours="+idType+";";

            rs = bdd.executer_requete(req);
            while(rs.next()){
                listeId.add(rs.getInt("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listeId;
    }

    public ArrayList<Integer> getidSeanceTpBDD(int idUE) {
        ArrayList<Integer> listeId = new ArrayList<Integer>();
        int idType;
        
        String req = "SELECT id FROM Type_Cours WHERE nom='TP';";
        ResultSet rs = bdd.executer_requete(req);
        try {
            rs.next();
            idType = rs.getInt("id");
        
            req = "SELECT id FROM Seance WHERE idUE = "+idUE+" AND idTypeCours="+idType+";";

            rs = bdd.executer_requete(req);
            while(rs.next()){
                listeId.add(rs.getInt("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listeId;
    }

    public int getIdPrecedentSeanceBDD(int idSeance) {
        Integer id = null;
        
        String req = "SELECT idSeancePrecedente FROM Seance WHERE id = "+idSeance+";";
        
        ResultSet rs = bdd.executer_requete(req);
        try {
            while(rs.next()){
                id = rs.getInt("idSeancePrecedente");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return id;
    }

    public int getDureeSeanceBDD(int idSeance) {
        Integer duree = null;
        
        String req = "SELECT duree FROM Seance WHERE id = "+idSeance+";";
        
        ResultSet rs = bdd.executer_requete(req);
        try {
            while(rs.next()){
                duree = rs.getInt("duree");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return duree;
    }

    public int getCapaciteSeanceBDD(int idSeance) {
        Integer capacite = null;
        Integer idSalle;
        
        String req = "SELECT idSalle FROM Seance WHERE id="+idSeance+";";
        ResultSet rs = bdd.executer_requete(req);
        try {
            rs.next();
            idSalle = rs.getInt("idSalle");
        
            req = "SELECT capacite FROM Salle WHERE id = "+idSalle+";";

            rs = bdd.executer_requete(req);
            rs.next();
            capacite = rs.getInt("capacite");
            
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return capacite;
    }

    public String getTypeSeanceBDD(int idSeance) {
        String type = null;
        Integer idTypeCours;
        
        String req = "SELECT idTypeCours FROM Seance WHERE id="+idSeance+";";
        ResultSet rs = bdd.executer_requete(req);
        try {
            rs.next();
            idTypeCours = rs.getInt("idTypeCours");
        
            req = "SELECT nom FROM Type_Cours WHERE id = "+idTypeCours+";";

            rs = bdd.executer_requete(req);
            rs.next();
            type = rs.getString("nom");
            
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return type;
    }

    public boolean getEffectueeSeanceBDD(int idSeance) {
        Boolean effectue = null;
        
        String req = "SELECT effectue FROM Seance WHERE id = "+idSeance+";";
        
        ResultSet rs = bdd.executer_requete(req);
        try {
            while(rs.next()){
                effectue = rs.getBoolean("effectue");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return effectue;
    }

    public ArrayList<Integer> getListeIdContrainteSalleSeanceBDD(int idSeance) {
        ArrayList<Integer> listeId = new ArrayList<Integer>();
        Integer idSalle;
        
        String req = "SELECT idSalle FROM Seance WHERE id="+idSeance+";";
        ResultSet rs = bdd.executer_requete(req);
        try {
            rs.next();
            idSalle = rs.getInt("idSalle");
        
            req = "SELECT id FROM Creneau_Salle WHERE idSalle = "+idSalle+";";

            rs = bdd.executer_requete(req);
            while(rs.next()){
               listeId.add(rs.getInt("id"));
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(Cours_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listeId;
    }
    
}
